package com.sby.dao;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sby.Controller.entity.InformationPage;
import com.sby.Controller.entity.realData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class InformationPageDaoImple implements InformationPageDao {
    private JdbcTemplate template;
    @Autowired
    public void setDataSource(DataSource dataSource) {
        template = new JdbcTemplate(dataSource);
    }
    @Override
    public List<InformationPage> findAll()  {
        URL url = null;
        try {
            url = new URL("http://192.168.0.222:3000");
            URLConnection conn = url.openConnection();
            conn.setConnectTimeout(10000);
            conn.connect();
            InputStream inStream = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(inStream,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            String s = null;
            List<String> list = new ArrayList<String >();
            while((s = br.readLine())!= null){
                list.add(s);
            }
            String s1 = "";
            for(int i = 0;i<list.size();i++){
                s1+=list.get(i);
            }
            //System.out.println(s1);
            //Json的解析类对象
            JsonParser parser = new JsonParser();
            //将JSON的String 转成一个JsonArray对象
            JsonArray jsonArray = parser.parse(s1).getAsJsonArray();
            Gson gson = new Gson();
            ArrayList<InformationPage> lists = new ArrayList<InformationPage>();
            //加强for循环遍历JsonArray
            for (JsonElement user : jsonArray) {
                //使用GSON，直接转成Bean对象
                InformationPage info = gson.fromJson(user,InformationPage.class);
                System.out.println(info.getId()+info.getName()+info.getProjectId());
                lists.add(info);
            }
            return lists;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            return null;
        }

    }

    @Override
    public List<realData> findRealData() {
        String sql = "select a.id,a.deviceName,b.tvalue,b.latitude,b.longitude,b.update_at from device a left  join realdata b on a.id=b.deviceId;";
        return this.template.query(sql, new RowMapper<realData>() {
            @Override
            public realData mapRow(ResultSet rs, int i) throws SQLException {
                realData real = new realData();
                real.setId(rs.getInt("id"));
                real.setDeviceName(rs.getString("deviceName"));
                real.setTvalue(rs.getString("tvalue"));
                real.setLatitude(rs.getString("latitude"));
                real.setLongitude(rs.getString("longitude"));
                real.setUpdateAt(rs.getString("update_at"));
                return real;
            }
        });
    }


}
